<template>
  <div class="h-full flex-col">
    <SearchForm @handle-reset="resetParams" @handle-search="getList">
      <template #header>
        <el-button @click="onAddOrUpdate()">新增子账号</el-button>
      </template>
      <el-form :model="searchForm" inline>
        <el-form-item>
          <el-input
            v-model="searchForm._id"
            clearable
            @change="getList"
            class="w-220px"
            placeholder="ID"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="searchForm.userName"
            clearable
            @change="getList"
            class="w-220px"
            placeholder="用户名搜索"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="searchForm.phone"
            clearable
            @change="getList"
            class="w-220px"
            placeholder="用户手机号"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="searchForm.proxyPhone"
            clearable
            @change="getList"
            class="w-220px"
            placeholder="父级用户手机号"
          />
        </el-form-item>

        <el-form-item>
          <el-select
            v-model="searchForm.status"
            placeholder="状态"
            @change="getList"
            clearable
            class="w-220px"
          >
            <el-option label="启用" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>
      </el-form>
    </SearchForm>
    <div class="page-card h-full" v-loading="loading">
      <el-table :data="tableData">
        <el-table-column label="_id" prop="_id" align="center" />
        <el-table-column label="名称" prop="userName" align="center" />
        <el-table-column label="手机号" prop="phone" align="center" />
        <el-table-column label="渠道码" prop="channelCode" align="center" />
        <!-- <el-table-column label="用户数量" prop="userNum" align="center">
          <template #default="{ row }">
            <span>{{ row.userNum || '0' }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="父级用户" align="center">
          <template #default="{ row }">
            <span>{{ (row.parentId && row.parentId.userName) || '/' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="父级手机号" align="center">
          <template #default="{ row }">
            <span>{{ (row.parentId && row.parentId.phone) || '/' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色" align="center" width="130">
          <template #default="{ row }">
            <el-tag>
              {{ row.roleName || '/' }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- admin adminSp有的权限 -->
        <el-table-column label="卡号" align="center">
          <template #default="{ row }">
            <span>{{ row.binkCard || '/' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开户行地址" align="center">
          <template #default="{ row }">
            <span>{{ row.binkLocation || '/' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付宝账号" align="center">
          <template #default="{ row }">
            <span>{{ row.aliAccount || '/' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付宝姓名" align="center">
          <template #default="{ row }">
            <span>{{ row.aliName || '/' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="返点(%)" align="center">
          <template #default="{ row }">
            <span>{{ row.point || '0' }}</span>
          </template>
        </el-table-column>
        <!-- admin adminSp有的权限 -->
        <el-table-column label="状态" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'info'">
              {{ row.status === 1 ? '正常' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" align="center" />
        <el-table-column label="操作" width="160" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="onAddOrUpdate(scope.row)">编辑</el-button>
            <el-button type="danger" link @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :pageable="page"
        :handleSizeChange="handleSizeChange"
        :handleCurrentChange="handleCurrentChange"
      />
      <AddOrUpdate ref="addOrUpdateRef" :getList="getList" :roleData="roleData" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useTable } from '@/hooks/useTable';
import { getAgentsUserListApi, deleteUserApi } from '@/api/user';
import { AddOrUpdate } from './components';
import type { IUserItem } from '@/api/user/type';
import { roleList } from '@/api/role';
import { useUserStore } from '@/store/modules/user';
import { useRouter } from 'vue-router';

const addOrUpdateRef = ref();
const searchForm = reactive<any>({
  phone: '',
  status: '',
  userName: '',
  _id: '',
  proxyPhone: ''
});
const router = useRouter();

const { id = '' } = router.currentRoute.value.query;

searchForm._id = id;

const { tableData, page, loading, resetParams, getList, handleCurrentChange, handleSizeChange } =
  useTable({
    apiFn: getAgentsUserListApi,
    params: searchForm
  });
let roleData = ref<any>([]);
let roleDataInfo = ref<any>([]);
const getRoleList = async () => {
  const { data } = await roleList();
  const userStore = useUserStore();
  const roleName = userStore.userInfo.roleName;
  roleData.value = [];
  if (roleName === 'adminSp') [(roleData.value = data.list)];
  if (roleName === 'admin') {
    roleData.value = data.list.filter((item: any) => item.role_name !== 'admin');
  }
  if (roleName === 'proxy1') {
    roleData.value = data.list.filter((item: any) => item.role_name === 'proxy2');
  }
  roleDataInfo.value = data.list;
};
getRoleList();
const onAddOrUpdate = (data?: IUserItem) => {
  addOrUpdateRef.value.init(data);
};

const onDelete = (row: any) => {
  ElMessageBox.confirm('您确认要删除当前项吗？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      deleteUserApi({ _id: row._id }).then((_) => {
        ElMessage({
          type: 'success',
          message: '删除成功'
        });
        getList();
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消操作'
      });
    });
};
</script>

<style lang="scss" scoped></style>
